package com.chujiekeji.test.activity;

import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;

import com.chujiekeji.test.R;
import com.chujiekeji.test.api.ChujieApi;
import com.chujiekeji.test.base.BaseActivity;
import com.chujiekeji.test.BaseApplication;
import com.chujiekeji.test.base.BaseConstant;
import com.chujiekeji.test.base.BaseToast;
import com.chujiekeji.test.bean.PageBean;
import com.chujiekeji.test.bean.ResultBean;
import com.chujiekeji.test.bean.Task;
import com.chujiekeji.test.bean.User;
import com.chujiekeji.test.util.AppOperator;
import com.chujiekeji.test.util.TLog;
import com.chujiekeji.test.view.CenterTextView;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.TextHttpResponseHandler;

import cz.msebera.android.httpclient.Header;

/**
 * @author MapleStory
 * @ qq 1002285057
 * @ project https://gitee.com/MapStory/Shopwt-Android
 */

public class RegisterActivity extends BaseActivity {

    private Toolbar mainToolbar;
    private AppCompatEditText vRealName;
    private AppCompatEditText vNickName;
    private AppCompatEditText vPassword;
    private AppCompatEditText vConfirmPassword;
    private AppCompatEditText vPhone;
    private AppCompatEditText vIdNum;
    private AppCompatEditText vEmail;
    private AppCompatTextView registerTextView;

    private long exitTimeLong;
    private Drawable normalDrawable, normalPressDrawable;
    private Drawable mobileDrawable, mobilePressDrawable;

    @Override
    public void initView() {

        setContentView(R.layout.activity_base_register);
        mainToolbar = findViewById(R.id.mainToolbar);
        vRealName = findViewById(R.id.vRealName);
        vNickName = findViewById(R.id.vNickName);
        vPassword = findViewById(R.id.vPassword);
        vConfirmPassword = findViewById(R.id.vConfirmPassword);
        vPhone = findViewById(R.id.vPhone);
        vIdNum = findViewById(R.id.vIdNum);
        vEmail = findViewById(R.id.vEmail);
        registerTextView=findViewById(R.id.registerTextView);

    }

    @Override
    public void initData() {

        exitTimeLong = 0L;
        normalDrawable = BaseApplication.get().getMipmap(R.mipmap.ic_register_normal, R.color.greyAdd);
        mobileDrawable = BaseApplication.get().getMipmap(R.mipmap.ic_register_mobile, R.color.greyAdd);
        normalPressDrawable = BaseApplication.get().getMipmap(R.mipmap.ic_register_normal_press);
        mobilePressDrawable = BaseApplication.get().getMipmap(R.mipmap.ic_register_mobile_press);

        setToolbar(mainToolbar, "会员注册");

    }

    @Override
    public void initEven() {


        registerTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });

    }

    @Override
    public void onReturn() {
            super.onReturn();
    }

    //自定义方法

    private void register() {
        BaseApplication.get().hideKeyboard(getActivity());

        String realName = vRealName.getText().toString();
        String nickName = vNickName.getText().toString();
        String phone = vPhone.getText().toString();
        String password = vPassword.getText().toString();
        String confirm = vConfirmPassword.getText().toString();
        String email = vEmail.getText().toString();
        String idNum = vIdNum.getText().toString();

        if (!password.equals(confirm)) {
            BaseToast.get().show("两次输入的密码不一致...");
            return;
        }
        registerTextView.setEnabled(false);
        registerTextView.setText("注册中...");
        ChujieApi.register(realName,nickName,phone,password,email,idNum,new TextHttpResponseHandler(){
            @Override
            public void onStart() {
                super.onStart();
            }
            @Override
            public void onFinish() {
                super.onFinish();
            }

            @Override
            public void onCancel() {
                super.onCancel();
            }
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                TLog.e("-----",responseString);
                registerTextView.setEnabled(true);
                registerTextView.setText("开始注册");
                BaseToast.get().show(responseString);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                ResultBean<PageBean<User>> resultBean = AppOperator.createGson().fromJson(responseString, new TypeToken<ResultBean>() {
                }.getType());
                TLog.i("-----",responseString);
                BaseToast.get().show(resultBean.getMessage());
                if (resultBean.getStatus()==1){
                    registerTextView.setEnabled(false);
                    registerTextView.setText("完成注册");
                    finish();
                }else {
                    registerTextView.setEnabled(true);
                    registerTextView.setText("开始注册");
                }
            }
        });

    }

}
